package com.pdool.game.util;

import com.google.protobuf.GeneratedMessageV3;
import com.pdool.api.constants.MsgDefine;
import com.pdool.game.mgr.UserMgr;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.nio.ByteBuffer;

public class PushUtil {

    public static void pushMsg(String roleId, int headId, GeneratedMessageV3 msg) throws IOException {
        WebSocketSession webSocketSession = UserMgr.getInstance().getSessionMap().get(roleId);
        if (webSocketSession != null && webSocketSession.isOpen()) {
            byte[] build = msg.toBuilder().build().toByteArray();
            ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + build.length);
            buffer.putInt(MsgDefine.LOGIN);
            buffer.put(build);
            BinaryMessage binaryMessage = new BinaryMessage(buffer.array());
            webSocketSession.sendMessage(binaryMessage);
        }
    }
}
